package com.kaili.basic.controller;


import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.MdmCityVO;
import com.kaili.basic.service.IBaseCityService;
import com.kaili.basic.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Validated
@Api(tags = "区域相关")
public class MdmCityController {

    @Autowired
    private IBaseCityService baseCityService;

    /**
     * 模糊查询省市区县(向上查询flag 1)
     * @param mdmCityLikeDto
     * @return
     */
    @ApiOperation(value = "模糊查询省市区县(向上查询flag 1)", notes = "模糊查询省市区县(向上查询flag 1)")
    @GetMapping("baseCite/like")
    public List<MdmCityVO> getAreaByName(@ModelAttribute MdmCityLikeDto mdmCityLikeDto ) {
        MdmCityVO mdmCityVO = new MdmCityVO();
        mdmCityVO.setCountyName(mdmCityLikeDto.getCountyName());
        mdmCityVO.setCityName(mdmCityLikeDto.getCityName());
        mdmCityVO.setProvinceName(mdmCityLikeDto.getProvinceName());
        if(StringUtils.isEmpty(mdmCityVO.getCountyName())) {
            if(StringUtils.isEmpty(mdmCityVO.getCityName()) ) {
                return baseCityService.getProvinceName(mdmCityVO);
            }else {
                return baseCityService.getCityName(mdmCityVO);
            }
        }
        return baseCityService.getAreaByName(mdmCityVO);
    }

    @ApiOperation(value = "获取区县", notes = "获取区县")
    @GetMapping("baseSite/county")
    public List<Map> getCounty() {
        return baseCityService.getCounty();
    }

    @ApiOperation(value = "获取城市", notes = "获取城市")
    @GetMapping("baseSite/city")
    public List<Map> getCity() {
        return baseCityService.getCity();
    }

    @ApiOperation(value = "获取省份", notes = "获取省份")
    @GetMapping("baseSite/province")
    public List<Map> getProvince() {
        return baseCityService.getProvince();
    }

    @ApiOperation(value = "获取地址", notes = "获取地址")
    @GetMapping("baseSite/area")
    public List<Map> getArea() {
        return baseCityService.getArea();
    }

    @ApiOperation(value = "根据名字获取城市", notes = "根据名字获取城市")
    @RequestMapping("/baseCityName/{name}")
    public String queryCityName(@RequestParam("name") String name) {
        MdmCityVO cityVO = baseCityService.selectCityByName(name);
        return cityVO.getCityId().toString();
    }

    @ApiOperation(value = "根据名字获取省市区", notes = "根据名字获取省市区")
    @GetMapping("/baseProvinceCityCounty/like")
    public List queryProvinceCityCounty(@RequestParam("name") String name) {
        List<MdmCityVO> mdmCityVOList = baseCityService.queryProvinceCityCounty(name);
        List listProvince = new ArrayList();
        List listCity;
        List listCounty;

        Map<Integer, List<MdmCityVO>> provinceGroup = mdmCityVOList.stream().collect(Collectors.groupingBy(MdmCityVO::getProvinceId));
        for (List<MdmCityVO> keyProvince : provinceGroup.values()) {
            listCity = new ArrayList();
            Map mapProvince = new HashMap();
            mapProvince.put("id",keyProvince.get(0).getProvinceId());
            mapProvince.put("code",keyProvince.get(0).getProvinceCode());
            mapProvince.put("name",keyProvince.get(0).getProvinceName());

            Map<Integer, List<MdmCityVO>> cityGroup = keyProvince.stream().collect(Collectors.groupingBy(MdmCityVO::getCityId));

            for (List<MdmCityVO> keyCity : cityGroup.values()) {
                listCounty = new ArrayList();
                Map mapCity = new HashMap();
                mapCity.put("id",keyCity.get(0).getCityId());
                mapCity.put("code",keyCity.get(0).getCityCode());
                mapCity.put("name",keyCity.get(0).getCityName());
                mapCity.put("province_id",keyCity.get(0).getProvinceId());

                for(MdmCityVO mdmCityVOCounty:keyProvince){
                    Map mapCounty = new HashMap();
                    if(mdmCityVOCounty.getCityId()==keyCity.get(0).getCityId()) {
                        mapCounty.put("id", mdmCityVOCounty.getCountyId());
                        mapCounty.put("code", mdmCityVOCounty.getCountyCode());
                        mapCounty.put("name", mdmCityVOCounty.getCountyName());
                        mapCounty.put("city_id", mdmCityVOCounty.getCityId());
                        listCounty.add(mapCounty);
                    }
                }
                mapCity.put("list",listCounty);
                listCity.add(mapCity);
            }

            mapProvince.put("list",listCity);
            listProvince.add(mapProvince);
        }
        return listProvince;
    }
}
